package com.gzgy.ustore.api.web.controller.webservice;

import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.exception.ExceptionCodeStatusEnum;
import com.gzgy.ustore.api.common.util.DateUtil;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.service.OrganizationService;
import com.gzgy.ustore.api.util.ResultUtil;
import com.gzgy.ustore.api.web.controller.BaseController;
import com.ustore.model.Organization;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.sql.SQLException;

@Controller
public class OrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = "orgListTree",method = RequestMethod.POST)
    public String orgListTree(ModelMap modelMap,String userId,String session,String companyId){

        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(organizationService.orgListTree(NumberUtils.toInt(companyId))));
        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "addOrganization",method = RequestMethod.POST)
    public String addOrganization(ModelMap modelMap, String userId, String session, Organization organization){

        Assert.notNull(organization.getCompanyId(),"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(organization.getOrgId(),"orgId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(organization.getOrgName(),"orgName " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(organization.getOrgType(),"orgType " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(organization.getHighOrg(),"highOrg " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(organization.getTreeCode(),"treeCode " + I18nUtil.getMessage(-2,null,"zh_CN"));

        organization.setCreatedBy(userId);
        organization.setCreatedTime(DateUtil.getCurrentDate());
        organization.setId(null);
        modelMap.put("message", ResultUtil.getResultJson(organizationService.addOrganization(organization)));
        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "orgOperation",method = RequestMethod.POST)
    public String orgOperation(ModelMap modelMap, String userId, String session, Organization organization,String flag) throws SQLException {


        Assert.notNull(organization.getId(),"id " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(flag,"flag " + I18nUtil.getMessage(-2,null,"zh_CN"));

        if("0".equals(flag)){
            Assert.hasText(organization.getOrgName(),"orgName " + I18nUtil.getMessage(-2,null,"zh_CN"));
            Assert.hasText(organization.getOrgType(),"orgType " + I18nUtil.getMessage(-2,null,"zh_CN"));

        }else if(!"1".equals(flag)){
            int errorCode = ExceptionCodeStatusEnum.UNKOWN_PARAMTER_EXCEPTION.toInt();
            String errorMsg = I18nUtil.getMessage(errorCode, null, "zh_CN");
            throw new BaseAPIException(errorMsg, errorCode);
        }

        organization.setUpdatedBy(userId);
        organization.setUpdatedTime(DateUtil.getCurrentDate());
        modelMap.put("message", ResultUtil.getResultJson(organizationService.orgOperation(organization,flag)));
        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "getOrgById",method = RequestMethod.POST)
    public String getOrgById(ModelMap modelMap, String userId, String session, String id) throws SQLException {


        Assert.hasText(id,"id " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(organizationService.getOrgById(id)));
        return SHOW_JSON_NAME;
    }
}
